package com.fizzkai.p2p.bussness.service.impl;

import com.fizzkai.p2p.bussness.domain.AccountFlow;
import com.fizzkai.p2p.bussness.mapper.AccountFlowMapper;
import com.fizzkai.p2p.bussness.service.IAccountFlowService;
import com.fizzkai.p2p.core.domain.Account;
import com.fizzkai.p2p.core.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {

    @Autowired
    private AccountFlowMapper accountFlowMapper;

    @Override
    public int save(AccountFlow entity) {
        return accountFlowMapper.insert(entity);
    }

    @Override
    public int update(AccountFlow entity) {
        return accountFlowMapper.updateByPrimaryKey(entity);
    }

    @Override
    public AccountFlow get(Long id) {
        return accountFlowMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AccountFlow> list() {
        return accountFlowMapper.selectAll();
    }



    @Override
    public void createRechargeOfflineFlow(Account account, BigDecimal amount, String remark) {
        createFlow(account, amount, "线下充值：" + amount + "元；" + remark, Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE);
    }
    @Override
    public void createAuditFailFlow(Account account, BigDecimal amount, String remark) {
        createFlow(account, amount, "满标审核_解冻退回：" + amount + "元；" + remark, Constants.ACCOUNT_ACTIONTYPE_BID_UNFREEZED);
    }

    @Override
    public void createBorrowSuccessFlow(Account account, BigDecimal amount, String remark) {
        createFlow(account, amount, "借款成功：" + amount + "元；" + remark, Constants.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL);
    }

    @Override
    public void createAccountManagementChargeFlow(Account account, BigDecimal amount, String remark) {
        createFlow(account, amount, "收取手续费成功：" + amount + "元；" + remark, Constants.ACCOUNT_ACTIONTYPE_CHARGE);

    }

    @Override
    public void createBidSuccessFlow(Account account, BigDecimal amount, String remark) {
        createFlow(account, amount, "投标成功：" + amount + "元；" + remark, Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL);
    }

    @Override
    public void createReturnMoneyFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, "还款成功：" + amount + "元；", Constants.ACCOUNT_ACTIONTYPE_RETURN_MONEY);
    }

    @Override
    public void createGainReturnMoneyFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, "还款成功：" + amount + "元；", Constants.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY);
    }

    @Override
    public void createPayInterestManagerChargeFlow(Account account, BigDecimal amount) {
        // 利息管理费
        createFlow(account, amount, "收取回款手续费：" + amount + "元；", Constants.ACCOUNT_ACTIONTYPE_INTEREST_SHARE);
    }


    private void createFlow(Account account, BigDecimal amount, String remark, int state) {
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setAccountId(account.getId());
        accountFlow.setActionTime(new Date());
        accountFlow.setActionType(state);
        accountFlow.setFreezedAmount(account.getFreezedAmount());
        accountFlow.setNote(remark);
        accountFlow.setUsableAmount(account.getUsableAmount());
        accountFlow.setAmount(amount);
        accountFlowMapper.insert(accountFlow);
    }

}
